package cn.lhl.web.controller.system;

import cn.lhl.domain.system.Module;
import cn.lhl.domain.system.Role;
import cn.lhl.service.system.ModuleService;
import cn.lhl.service.system.RoleService;
import cn.lhl.web.controller.BaseController;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("/system/role")
public class RoleController extends BaseController {
    //注入RoleDao
    @Autowired
    private RoleService roleService;

    @Autowired
    private ModuleService moduleService;

    @RequestMapping(value = "/list", name = "分页查询所有")
    public String findAll(@RequestParam(defaultValue = "1") int page,
                          @RequestParam(defaultValue = "5") int size) {
        //调用Service
        PageInfo pageInfo = roleService.findAll(page, size, companyId);//此companyId由继承的BaseController得来
        model.addAttribute("page", pageInfo);
        return "/system/role/role-list";
    }

    @RequestMapping(value = "/toAdd", name = "去新增界面")
    public String toAdd() {
        return "/system/role/role-add";
    }

    @RequestMapping(value = "/toUpdate", name = "去修改界面")
    public String toEdit(String id) {
        //需要获取改角色的信息
        Role role = roleService.findById(id);
        model.addAttribute("role", role);
        return "/system/role/role-update";
    }

    //编辑,根据是否有role_id判断其请求是新增还是修改
    @RequestMapping(value = "/edit", name = "修改或新增")
    public String edit(Role role) {
        role.setCompanyId(companyId);
        role.setCompanyName(companyName);
        if (StringUtil.isEmpty(role.getId())) {
            //新增
            role.setId(UUID.randomUUID().toString());
            roleService.add(role);
        } else {
            //修改
            roleService.update(role);
        }
        return "redirect:/system/role/list.do";
    }

    @RequestMapping(value = "/delete", name = "删除角色")
    public String delete(String id) {
        roleService.delete(id);
        return "redirect:/system/role/list.do";
    }

    @RequestMapping(value = "/roleModule", name = "去管理权限界面")
    //获得角色的id,以此来得出是哪个角色名称在修改
    public String roleModule(String id) {
        Role role = roleService.findById(id);
        model.addAttribute("role", role);
        return "/system/role/role-module";
    }


    @RequestMapping(value = "/initModuleData", name = "想前台传递权限的参数")
    public @ResponseBody
    List<Map> initModuleData(String id) {
        //需要知道所有权限模块
        List<Module> allModuleList = moduleService.findAll();
        //根据角色id查询出模块信息(此信息中包含许多前台需要的数据)
        List<Module> moduleList = roleService.findByRoleId(id);
        /**
         * [{ id:11, pId:1, name:"随意勾选 1-1"},
         { id:111, pId:11, name:"随意勾选 1-1-1"}]
         */
        //返回的结果是上面的格式,以便前端操作
        //遍历
        List<Map> list = new ArrayList<Map>();
        for (Module module : allModuleList) {
            Map map = new HashMap();
            map.put("id", module.getId());
            map.put("pId", module.getParentId());
            map.put("name", module.getName());

            //添加"true"
            if (moduleList.contains(module)) {
                map.put("checked", true);
            }
            list.add(map);
        }
        return list;
    }

    @RequestMapping(value = "/updateRoleModule", name = "更新角色权限")
    public String updateRoleModule(String roleid, String moduleIds) {
        roleService.upDateRoleModule(roleid, moduleIds);
        return "redirect:/system/role/list.do";
    }
}
